package com.evan.review.viewpager;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.evan.review.R;

/**
 * 懒加载Fragment示例
 * 演示了在ViewPager中如何实现延迟加载数据
 */
public class LazyFragment extends Fragment {
    
    private static final String TAG = "LazyFragment";
    private static final String ARG_PAGE_NUMBER = "page_number";
    private static final String ARG_BACKGROUND_COLOR = "background_color";
    
    private int pageNumber; // 页面编号
    private int backgroundColor; // 背景颜色
    private boolean isDataLoaded = false; // 数据是否已加载
    private boolean isVisibleToUser = false; // 是否对用户可见
    
    private View rootView; // 根视图
    private TextView tvPageNumber; // 页面编号文本
    private TextView tvStatus; // 状态文本
    private ProgressBar progressBar; // 进度条
    
    /**
     * 创建LazyFragment的新实例
     * @param pageNumber 页面编号
     * @param backgroundColor 背景颜色
     * @return 新的LazyFragment实例
     */
    public static LazyFragment newInstance(int pageNumber, int backgroundColor) {
        LazyFragment fragment = new LazyFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE_NUMBER, pageNumber);
        args.putInt(ARG_BACKGROUND_COLOR, backgroundColor);
        fragment.setArguments(args);
        Log.d(TAG, "newInstance: 页面编号=" + pageNumber);
        return fragment;
    }
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            pageNumber = getArguments().getInt(ARG_PAGE_NUMBER);
            backgroundColor = getArguments().getInt(ARG_BACKGROUND_COLOR);
        }
        Log.d(TAG, "onCreate: 页面编号=" + pageNumber);
    }
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: 页面编号=" + pageNumber);
        rootView = inflater.inflate(R.layout.fragment_lazy, container, false);
        rootView.setBackgroundColor(backgroundColor);
        
        tvPageNumber = rootView.findViewById(R.id.tv_page_number);
        tvStatus = rootView.findViewById(R.id.tv_status);
        progressBar = rootView.findViewById(R.id.progress_bar);
        
        tvPageNumber.setText("页面 " + pageNumber);
        updateLoadingState();
        
        return rootView;
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG, "onViewCreated: 页面编号=" + pageNumber);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: 页面编号=" + pageNumber);
        if (isVisibleToUser && !isDataLoaded) {
            loadData();
        }
    }
    
    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: 页面编号=" + pageNumber);
    }
    
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach: 页面编号=" + pageNumber);
    }
    
    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG, "onDetach: 页面编号=" + pageNumber);
    }
    
    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: 页面编号=" + pageNumber);
    }
    
    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: 页面编号=" + pageNumber);
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "onDestroyView: 页面编号=" + pageNumber);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: 页面编号=" + pageNumber);
    }
    
    // 用于ViewPager1 - 在ViewPager2中已废弃，但为了学习目的仍然包含
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        Log.d(TAG, "setUserVisibleHint: 页面编号=" + pageNumber + ", 对用户可见=" + isVisibleToUser);
        
        if (isVisibleToUser && isResumed() && !isDataLoaded) {
            loadData();
        }
    }
    
    /**
     * 同时支持ViewPager1和ViewPager2的可见性变化处理
     * 更新加载状态的显示
     */
    private void updateLoadingState() {
        if (isDataLoaded) {
            tvStatus.setText("页面" + pageNumber + "的数据已加载");
            progressBar.setVisibility(View.GONE);
        } else {
            tvStatus.setText("页面" + pageNumber + "的数据未加载");
            progressBar.setVisibility(View.INVISIBLE);
        }
    }
    
    /**
     * 加载数据
     * 模拟网络请求或数据加载过程
     */
    private void loadData() {
        Log.d(TAG, "loadData: 开始加载页面" + pageNumber + "的数据");
        progressBar.setVisibility(View.VISIBLE);
        tvStatus.setText("正在加载页面" + pageNumber + "的数据...");
        
        // 模拟网络请求或数据加载
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            if (isAdded()) {
                isDataLoaded = true;
                Log.d(TAG, "loadData: 页面" + pageNumber + "的数据加载完成");
                updateLoadingState();
            }
        }, 1500);
    }
} 